//
//  MYGHomePageHeaderView.swift
//  MYDoctor
//
//  Created by Mo on 2024/3/6.
//

import UIKit
import QMUIKit

class MYGHomePageTitleView: UIView {
    override var intrinsicContentSize: CGSize {
        return CGSize(width: screenWidth, height: navHeight)
    }
    var userHeaderView:UIImageView = {
        let control = UIImageView()
        control.layer.masksToBounds = true
        control.layer.cornerRadius = 15
        control.sd_setImage(with:URL(string: MYGUserManager.getUser()?.doctor.avatar), placeholderImage: UIImage.init(named: "head"), context: nil)
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    var userNameLabel:UILabel = {
        let control = UILabel()
        control.textColor = RGBA(r: 51, g: 51, b: 51, a: 1)
        control.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        control.text = MYGUserManager.getUser()?.doctor.name
        return control
    }()
    var userIdentityLabel:UILabel = {
        let control = UILabel()
        control.textColor = RGBA(r: 51, g: 51, b: 51, a: 1)
        control.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        control.text = MYGUserManager.getUser()?.doctor.department
        control.backgroundColor = .white
        control.layer.masksToBounds = true
        control.layer.cornerRadius = 10
        return control
    }()
    lazy var moreBtn:UIButton = {
        let control = UIButton()
        control.setImage(UIImage(named: "MYG_home_more"), for: .normal)
        control.addTarget(self, action: #selector(toMore), for: .touchUpInside)
        return control
    }()
    
    lazy var searchBtn:UIButton = {
        let control = UIButton()
        control.setImage(UIImage(named: "MYG_home_search"), for: .normal)
        control.addTarget(self, action: #selector(toSearch), for: .touchUpInside)
        return control
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        self.addSubview(userHeaderView)
        self.addSubview(userNameLabel)
        self.addSubview(userIdentityLabel)
        self.addSubview(moreBtn)
        self.addSubview(searchBtn)

        userHeaderView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(6)
            make.top.equalToSuperview().offset(5)
            make.width.height.equalTo(30)
        }
        userNameLabel.snp.makeConstraints { make in
            make.left.equalTo(userHeaderView.snp.right).offset(7.5)
            make.centerY.equalTo(userHeaderView)
            make.height.equalTo(22.5)
        }
        userIdentityLabel.snp.makeConstraints { make in
            make.left.equalTo(userNameLabel.snp.right).offset(8.5)
            make.centerY.equalTo(userHeaderView)
            make.height.equalTo(20)
        }
        moreBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-4)
            make.width.height.equalTo(28)
            make.top.equalToSuperview().offset(5)
        }
        searchBtn.snp.makeConstraints { make in
            make.right.equalTo(moreBtn.snp.left).offset(-14.5)
            make.width.height.equalTo(28)
            make.top.equalToSuperview().offset(5)
        }
    }
    
    @objc func toMore() {
        let vc = MYGSystemMessageController()
        vc.isShowSchedule = false
        if let nav = self.rootResponsder() as? MYGNavigationController {
            nav.pushViewController(vc, animated: true)
        }
    }
    
    @objc func toSearch() {
        let vc = MYGHomePageSearchVC()
        if let nav = self.rootResponsder() as? MYGNavigationController {
            nav.pushViewController(vc, animated: true)
        }
    }
}

class MYGHomePageHeaderView: UIView {
    var userHeaderView:UIImageView!
    var userNameLabel:UILabel!
    var userIdentityLabel:UILabel!
    var moreBtn:UIButton!
    var searchBtn:UIButton!
    var scrollLabel: HSScrollLabel = {
        let control = HSScrollLabel()
        return control
    }()
    
    var tipLB:UILabel = {
        let control = UILabel()
        control.text = "日程提醒"
        control.textColor = UIColor.init(hexString: "#1772EF")
        control.textAlignment = .center
        control.layer.borderWidth = 0.5
        control.layer.cornerRadius = 2
        control.font = .regular(12)
        control.layer.borderColor = UIColor.init(hexString: "#5AA0FF").cgColor
        control.layer.masksToBounds = true
        return control
    }()
    
    var perGrowthBtn:homePageFuncBtn!
    var scienSupportBtn:homePageFuncBtn!
    var communitBtn:homePageFuncBtn!
    var effToolBtn:homePageFuncBtn!
    
    var toosBackView:toolsView!
    var toolsBlock:voidBlock?

    override init(frame: CGRect) {
        super.init(frame: frame)
        initUi()
        setUICon()
    }
    
    func initUi(){
        perGrowthBtn = homePageFuncBtn()
        perGrowthBtn.pageType = 0
        perGrowthBtn.addTarget(self, action: #selector(tapPerFrow(_ :)), for: .touchUpInside)
        
        scienSupportBtn = homePageFuncBtn()
        scienSupportBtn.addTarget(self, action: #selector(tapPerSience(_ :)), for: .touchUpInside)
        scienSupportBtn.pageType = 1
        
        communitBtn = homePageFuncBtn()
        communitBtn.addTarget(self, action: #selector(tapPerCommunit(_ :)), for: .touchUpInside)
        communitBtn.pageType = 2
        
        effToolBtn = homePageFuncBtn()
        effToolBtn.addTarget(self, action: #selector(tapTool), for: .touchUpInside)
        effToolBtn.pageType = 3
        
        toosBackView = toolsView()
        toosBackView.toolsActionBlock = {[weak self] in
            if self?.toolsBlock != nil{
                self?.toolsBlock!()
            }
        }
    }
    
    func bindData(models:[MYGHomeScheduleModel]) {
        var textArray = [String]()
        for(_,obj) in models.enumerated() {
            textArray.append(obj.name)
        }
        self.scrollLabel.textDatas = textArray
    }
    func setUICon(){
        let marginTop = 40
        
        addSubview(tipLB)
        tipLB.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(24)
            make.size.equalTo(CGSize.init(width: 55, height: 16))
            make.top.equalToSuperview().offset((12))
        }
        addSubview(scrollLabel)
        scrollLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.right.equalToSuperview().offset(-20)
            make.left.equalTo(tipLB.snp.right).offset(10)
            make.height.equalTo(marginTop)
        }
        self.addSubview(perGrowthBtn)
        self.addSubview(scienSupportBtn)
        self.addSubview(communitBtn)
        self.addSubview(effToolBtn)
        self.addSubview(toosBackView)
     
        let marginLeft = 18.0
        let btnWidth = (screenWidth - 3 * marginLeft)/2
        perGrowthBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(marginLeft)
            make.top.equalToSuperview().offset(marginTop)
            make.width.equalTo(btnWidth)
            make.height.equalTo(100)
        }
        
        scienSupportBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-marginLeft)
            make.top.equalToSuperview().offset(marginTop)
            make.width.equalTo(btnWidth)
            make.height.equalTo(100)
        }
        
        communitBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(marginLeft)
            make.top.equalTo(perGrowthBtn.snp.bottom).offset(marginLeft)
            make.width.equalTo(btnWidth)
            make.height.equalTo(100)
        }
        
        effToolBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-marginLeft)
            make.top.equalTo(perGrowthBtn.snp.bottom).offset(marginLeft)
            make.width.equalTo(btnWidth)
            make.height.equalTo(100)
        }
        toosBackView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(communitBtn.snp.bottom).offset(15)
            make.height.equalTo(140)
        }
    }
    //个人成长
    @objc func tapPerFrow(_ sender:UIButton) {
        let vc = MYGPersionGrowViewController()
        self.rootResponsder().navigationController?.pushViewController(vc, animated: true)

    }
    //行业交流
    @objc func tapPerCommunit(_ sender:UIButton) {
        let vc = MYGIndustryExchangesVC()
        self.rootResponsder().navigationController?.pushViewController(vc, animated: true)
    }
    @objc func tapPerSience(_ sender:UIButton) {
        let vc = MYGKeyanzhichiViewController()
        self.rootResponsder().navigationController?.pushViewController(vc, animated: true)
    }
    //效率工具
    @objc func tapTool() {
        self.rootResponsder().navigationController?.pushViewController(MYGToolRootVC(), animated: true)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
class homePageFuncBtn: UIButton {
    var titleLab:UILabel!
    var JImageV:UIImageView!
    
    var pageType:Int = 0{
        didSet{
            switch pageType{
            case 0:
                initUITypeOne()
                self.setBackgroundImage(UIImage(named: "MYG_home_per"), for: .normal)
//                self.setImage(UIImage(named: "MYG_home_per"), for: .normal)
                titleLab.text = "个人成长"
                JImageV.image = UIImage(named: "MYG_home_perJ")
                break
            case 1:
                initUITypeOne()
                self.setBackgroundImage(UIImage(named: "MYG_home_scien"), for: .normal)
//                self.setImage(UIImage(named: "MYG_home_scien"), for: .normal)
                titleLab.text = "科研支持"
                JImageV.image = UIImage(named: "MYG_home_scienJ")
                break
            case 2:
                initUITypeTwo()
                self.setBackgroundImage(UIImage(named: "MYG_home_commu"), for: .normal)
//                self.setImage(UIImage(named: "MYG_home_commu"), for: .normal)
                titleLab.text = "行业交流"
                JImageV.image = UIImage(named: "MYG_home_commuJ")
                break
            case 3:
                initUITypeOne()
                self.setBackgroundImage(UIImage(named: "MYG_home_eff"), for: .normal)
//                self.setImage(UIImage(named: "MYG_home_eff"), for: .normal)
                titleLab.text = "效率工具"
                JImageV.image = UIImage(named: "MYG_home_effJ")
                break
            default:
                break
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLab = UILabel()
        titleLab.textColor = .white
        titleLab.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        JImageV = UIImageView()
    }
    
    func initUITypeOne(){
        self.addSubview(titleLab)
        self.addSubview(JImageV)
        
        titleLab.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(21)
            make.top.equalToSuperview().offset(40.5)
            make.height.equalTo(18)
        }
        
        JImageV.snp.makeConstraints { make in
            make.left.equalTo(titleLab.snp.right).offset(11)
            make.centerY.equalTo(titleLab)
            make.width.equalTo(35)
            make.height.equalTo(21.5)
        }
    }
    func initUITypeTwo(){
        self.addSubview(titleLab)
        self.addSubview(JImageV)
        
        titleLab.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(14)
            make.top.equalToSuperview().offset(26)
            make.height.equalTo(18)
        }
        
        JImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(14)
            make.top.equalTo(titleLab.snp.bottom).offset(10.5)
            make.width.equalTo(35)
            make.height.equalTo(21.5)
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class toolsView: UIView {
    
    var toolsActionBlock:voidBlock?
    private var contents: [MYGHomeToolsItem] = []
    
    @objc
    private func configCell() {
        contents = []
        var home: [MYGHomeToolsType] = []
        let dic = UserDefaults.standard.value(forKey: "HomeSortDefalut") as? [String: [Int]] ?? [:]
        if let array = dic["home"]?.map({MYGHomeToolsType(rawValue: $0) ?? .more}) as? [MYGHomeToolsType] {
            home = array
        } else {
            home = [.bodyImage, .operation, .medical,.study]
        }
        _ = MYGHomeToolsSeciton(name: "首页应用")
        contents = Array(home.map({ type in
            let item = MYGHomeToolsItem(type: type, isHome: true)
            return item
        })[0...3])
        collectionView.reloadData()
    }
         
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    func setUI(){
        NotificationCenter.default.addObserver(self, selector: #selector(configCell), name: NSNotification.Name.init("HomeSortChange"), object: nil)
        configCell()
        
        self.backgroundColor = .white
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 5
        self.layer.shadowColor = RGBA(r: 0, g: 0, b: 0, a: 0.08).cgColor
        self.layer.shadowOpacity = 1
        self.layer.shadowRadius = 12
        self.layer.shadowOffset = CGSize(width: 0, height: 1)
        let typeImageV = UIImageView()
        typeImageV.image = UIImage(named: "MYG_home_type")
        self.addSubview(typeImageV)
        typeImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(17.5)
            make.width.equalTo(3.5)
            make.height.equalTo(15.5)
        }
        let typeNameLabel = UILabel()
        typeNameLabel.textColor = RGBA(r: 51, g: 51, b: 51, a: 1)
        typeNameLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        typeNameLabel.text = "我的工具"
        self.addSubview(typeNameLabel)
        typeNameLabel.snp.makeConstraints { make in
            make.left.equalTo(typeImageV.snp.right).offset(10.5)
            make.centerY.equalTo(typeImageV)
            make.height.equalTo(25)
        }
        
        let moreBtn = UIButton()
        moreBtn.setImage(UIImage(named: "MYG_home_moreTool"), for: .normal)
        moreBtn.addTarget(self, action: #selector(toolsMoreAction), for: .touchUpInside)
        self.addSubview(moreBtn)
        moreBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-19.5)
            make.top.equalToSuperview().offset(15)
            make.width.height.equalTo(21)
        }
        
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(typeNameLabel.snp.bottom)
            make.height.equalTo(96)
        }
    }
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (screenWidth - 60)/4, height: 96)
        layout.sectionInset = .init(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.delegate = self
        cv.dataSource = self
        cv.register(cellWithClass: MYGToolsCollectionViewCell.self)
        cv.backgroundColor = .white
        return cv
    }()
    
    @objc func toolsMoreAction(){
        if self.toolsActionBlock != nil{
            self.toolsActionBlock!()
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension toolsView: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        contents.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: MYGToolsCollectionViewCell.self, for: indexPath)
        cell.model = contents[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        contents[indexPath.row].action?()
    }
}
